package com.aaa.system.service.impl;

import com.aaa.entity.OperLog;
import com.aaa.system.dao.OperLogMapper;
import com.aaa.system.service.IOperLogService;
import com.aaa.system.vo.OperLogVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;

/**
 * <p>
 * 操作日志记录 服务实现类
 * </p>
 *
 * @author L
 * @since 2023-03-23
 */
@Service
public class OperLogServiceImpl extends ServiceImpl<OperLogMapper, OperLog> implements IOperLogService {
    @Autowired
    private OperLogMapper operLogMapper;

    @Override
    public void insertOperLog(OperLog operLog) {
        int insert = operLogMapper.insert(operLog);
    }

    @Override
    public Result<IPage<OperLog>> selectAll(Integer current, Integer size, OperLogVo operLogVo) {
        IPage<OperLog> page = new Page<>(current,size);
        QueryWrapper<OperLog> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(operLogVo.getTitle())){
            queryWrapper.like("title",operLogVo.getTitle());
        }
        if (StringUtils.hasText(operLogVo.getOperName())){
            queryWrapper.like("oper_name",operLogVo.getOperName());
        }
        if (Objects.nonNull(operLogVo.getBusinessType())){
            queryWrapper.eq("business_type",operLogVo.getBusinessType());
        }
        if (Objects.nonNull(operLogVo.getStatus())){
            queryWrapper.eq("status",operLogVo.getStatus());
        }
        if (Objects.nonNull(operLogVo.getDateRange()) && operLogVo.getDateRange().length==2){
            queryWrapper.between("oper_time",operLogVo.getDateRange()[0],operLogVo.getDateRange()[1]);
        }
        IPage<OperLog> operLogIPage = operLogMapper.selectPage(page,queryWrapper);
        return new Result<>(200,"查询所有操作日志成功",operLogIPage);
    }

    @Override
    public Result<Boolean> deleteOperLogByIds(long[] operIds) {
        Boolean b = null;
        if (operIds.length>0){
            b = operLogMapper.batchDelById(operIds);
        }
        return new Result<>(200,"删除成功",b);
    }

    @Override
    public Result<Boolean> truncateTable() {
        Boolean b = operLogMapper.truncateTable();
        return new Result<>(200,"成功清空所有操作日志",b);
    }
}
